/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Query;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import pojo.LiveServers;

/**
 *
 * @author A7med Kotb
 */
public class LiveServerHistoryAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception 
    {    
        String searchtext = request.getParameter("searchtext");
        System.out.println("Search Text TTTTTTTTTTTTTTTTTTT " +searchtext);
        String out = "";
        if (searchtext!=null && !searchtext.equals("")) {
            Query q = new Query();
            Vector<LiveServers> result = q.getByLiveServerName(getDataSource(request, "MyDS").getConnection(),searchtext);
            if (result != null && result.size() > 0) 
            {
                for (int i = 0; i < result.size(); i++) {
                    out += result.get(i).getIp() + ",";
                }
                out = out.substring(0, out.length() - 1);
            } else {
                out = " ";
            }

        } else {
            
          Query q = new Query();
            Vector<String> result = q.getProviders(getDataSource(request, "MyDS").getConnection());
            if (result != null && result.size() > 0) {
                for (int i = 0; i < result.size(); i++) {
                    out += result.get(i) + ",";
                }
                out = out.substring(0, out.length() - 1);
            } else {
                out = " ";
            }
        }
        response.getWriter().println(out);
        return null;
    }
}
